var layer;
var $;
var table;
var form;
layui.use(['element','layer','table','form'], function(){
    layer = layui.layer;
    lement = layui.element;//面包导航
    $ = layui.jquery;
    table = layui.table;
    form=layui.form;
    //客服展示数据
    selSmartAI();
    //刷新
    load();
    //获取接待人数
    selReceptionNum();
});
/*
* 获取接待人数
* */
function selReceptionNum(){
    $.get("/personnel/selReceptionNum", {}, function (res) {
       //客服今日接待人数
        $("#today").text(res.data.selTodayNum);
        //客服昨日接待人数
        $("#yes").text(res.data.selYesterdayNum);
        //客服本月接待人数
        $("#mou").text(res.data.selThirtyDays);
        //客服总接待人数
        $("#num").text(res.data.selWholeNum);
    });
}
/*
* 删除聊天记录
* */
function delSmartAI(id){
    $.post("/personnel/delSmartAI", {id:id}, function (res) {
        if(res.data==1){
            layer.msg("操作成功",{icon:1});
            selSmartAI();
        }else{
            layer.msg("操作失败",{icon:1});
        }

    });
}
/*
* 获取查看记录数据
* */
function selRecord(id){
    $.post("/personnel/selRecord", {id:id}, function (res) {
        SmartAILayerSel(res.data);
    });
}

function SmartAILayerSel(data){
    var index = layer.open({
        type:2,//弹出层的类型
        title:"客服详情",
        area:['800px','550px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/SmartAI_layer_sel.html",
        btn:['关闭'],
        yes:function(index,layero){
            layer.closeAll('iframe');
        },success:function(layero,index){

            var frameId = $(layero).find("iframe").attr('id'); //获得子页面的id
            var iframeWindow = layero.find('iframe')[0].contentWindow; //获得子页面的对象
            //客服今日接待人数
            var today=$("#today").text();
            //客服昨日接待人数
            var yes=$("#yes").text();
            //客服本月接待人数
            var mou=$("#mou").text();
            //客服总接待人数
            var num=$("#num").text();
            var reception="<td>"+today+"</td><td>"+yes+"</td><td>"+mou+"</td><td>"+num+"</td>"
            //赋值table数据
            $(window.frames[frameId].document).find(".tab_tr1").html(reception);


            //赋值当前客服
            if(data.list[0].type==3){

                $(window.frames[frameId].document).find(".service").text(data.list[0].fromNickname);
                $.ajax({
                    url:"/personnel/selReception",
                    type:"post",
                    dataType:"json",
                    data:{id:data.list[0].from_id},
                    success:function(data) {
                        selReception(data.data,layero);
                    }
                })
            }else{

                $(window.frames[frameId].document).find(".service").text(data.list[0].toNickname);
                $.ajax({
                    url:"/personnel/selReception",
                    type:"post",
                    dataType:"json",
                    data:{id:data.list[0].to_id},
                    success:function(data) {
                        selReception(data.data,layero);
                    }
                });
            }


            var l=chatData(data.list,data.path)
            $(window.frames[frameId].document).find(".box").html(l);

            form.render();
        }
    })
    layer.full(index);
}

/*
* 循环拼接聊天数据
* */
function chatData(data,path){
    var a="";
    for(var i=0;i<data.length;i++){
        var f=data[i]
        if(f.type==3){
            a+= "<div class='chart-timer'>"+renderTime(f.send_time)
                +"</div>"
                +"<div class='item right'>"
                +"<img class='header-img' src='"+path+f.fromAvatar_url+"' />"
                +"<span class='message'>"+f.content+"</span>"
                +"</div>"
        }else{
            a+= "<div class='chart-timer'>"+renderTime(f.send_time)
                +"</div>"
                +"<div class='item left'>"
                +"<img class='header-img' src='"+path+f.fromAvatar_url+"' />"
                +"<span class='message'>"+f.content+"</span>"
                +"</div>"
        }
    }
    return a;
}
/*
* 循环拼接tab选项卡数据
* */
function TabAssignment(data,path){
    var a="";
    for(var i=0;i<data.length;i++){
        var f=data[i];
        a+= "<div class='item left'>"
            + "<img class='header-img' src="+path+f.fromAvatar_url+" />"
            + "<span class='message'>"+f.content+"</span>"
            + "</div>"
    }
    return a;
}
/*
* 获取当前客服的接待记录
* */
function selReception(rel,layero){
    var frameId = $(layero).find("iframe").attr('id'); //获得子页面的id
    var iframeWindow = layero.find('iframe')[0].contentWindow; //获得子页面的对象

    //赋值今天
    var today=TabAssignment(rel.selToday,rel.path);
    $(window.frames[frameId].document).find(".today").html(today);
    //赋值昨天
    var Yesterday=TabAssignment(rel.selYesterday,rel.path);
    $(window.frames[frameId].document).find(".Yesterday").html(Yesterday);
    //赋值本月
    var ThirtyDays=TabAssignment(rel.selThirtyDays,rel.path);
    $(window.frames[frameId].document).find(".ThirtyDays").html(ThirtyDays);
    //赋值全部
    var Whole=TabAssignment(rel.selWhole,rel.path);
    $(window.frames[frameId].document).find(".Whole").html(Whole);
    form.render();
}

/**
 *   客服展示数据
 * */
function selSmartAI(){
    $.ajax({
        url:"/personnel/selSmartAI",
        type:"get",
        dataType:"json",
        data:{},
        success:function(data) {
            selSmartAIData(data.data);
            console.log(data.data);
        }
    })
}
/**
 *   点击事件
 * */
function load(){
    //刷新页面
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
}
/**
 * 客服数据列表
 * */
function selSmartAIData(data){
    var table = layui.table;
    table.render({
        elem:"#userListTab",//容器
        title:"客服数据列表",
        data:data,
        page:true,
        cols:cols
    });
}
//layui-table格式
var cols=[[//定义列
    {type:'checkbox',align:'center',width:40},//复选框靠左
    {title:'序号',align:'center',width:60,templet:function(data){
            return data.LAY_INDEX;
        }},
    {field:"toNickname",title:"发送人",align:'center',width:180},
    {field:"toNickname",title:"接收人",align:'center',width:180},
    {field:"content",title:"消息内容",align:'center',width:180},
    {field:"send_time",title:"发送时间",align:'center',width:180,templet:function(data){
          return renderTime(data.send_time)
        }},
    {field:"type",title:"类型",align:'center',width:160,templet:function(data){
        if(data.type==2){
            return "提问";
        }else if(data.type==3){
            return "解答";
        }else{
            return "通知";
        }
        }},
    {field:"",title:"操作",align:'center',templet:function(data){
            return "<a style='color:#009688;cursor:pointer;' onclick='selRecord(\""+data.id+"\")'>查看记录</a> "+
                "<a style='color:#009688;cursor:pointer;' onclick='delSmartAI(\""+data.id+"\")'>删除记录</a> ";
        }}
]];
